﻿//ok
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using GameProEngine;

namespace ProjectoClase3
{
    class inGameScene:Scene
    {
        List<Keys> InputKeys = new List<Keys>();//en input manager

        public  List<Actor> actors = new List<Actor>();
        public  List<Actor> actorsToRemove = new List<Actor>();
        public  List<Actor> actorsToAdd = new List<Actor>();


        //private Tools herramientas= new Tools();
        //en input manager
        Keys[][] Combos = {new Keys[]{Keys.Right, Keys.Right},
                           new Keys[]{Keys.Left, Keys.Right, Keys.B},
                           new Keys[]{Keys.Down, Keys.Right, Keys.A},
                           new Keys[]{Keys.Left, Keys.Down, Keys.Right, Keys.A}};
        Actor tipito,tipito2,tipito3,tipito4;//lista
        //InputManager _inputManager; no es parte de ninguna escena, se lo mandamos a la escena que corresponda desde el sceneManager
        SpriteFont _debugFont;
        public inGameScene(ContentManager Content)
        {
        
            _debugFont = Content.Load<SpriteFont>("SpriteFont1");


            actors.Add(tipito = new Actor(new Animation(Content.Load<Texture2D>("spritesheet"), 8, 8, 60, 15), new Vector2(100, 100), new Vector2(2, 2)));
            actors.Add(tipito2 = new Actor(new Animation(Content.Load<Texture2D>("spritesheet"), 8, 8, 60, 15), new Vector2(400, 200), new Vector2(6, -2)));
            actors.Add(tipito3= new Actor(new Animation(Content.Load<Texture2D>("spritesheet"), 8, 8, 60, 15), new Vector2(300, 300), new Vector2(-2, 3)));
            actors.Add(tipito4 = new Actor(new Animation(Content.Load<Texture2D>("spritesheet"), 8, 8, 60, 15), new Vector2(500, 300), new Vector2(2, 5)));
            
            //tipito.tex = Content.Load<Texture2D>("tipito");
            
            //CreateActor();//TEMPORAL!!!
            //_inputManager = new InputManager(InputKeys, Combos);
            InputKeys.Add(Keys.Left);
            InputKeys.Add(Keys.Right);
            InputKeys.Add(Keys.Up);
            InputKeys.Add(Keys.Down);
            InputKeys.Add(Keys.A);
            InputKeys.Add(Keys.B);
        }
        public override void update(int dTime)
        {
            for (int i = 0; i < actors.Count; i++)
            {

                if (actors[i].isEnable == true)
                {
                    actors[i].update(dTime);
                    actors[i].anim.update(dTime);
                    
                    
                }
                //---------------------------tengo que hacer colision entre i y el proximo siempre y cuando i sea menor al ante ultimo.
                //desìes de cheaquear colision llamo al is colliding de cada uno.
               
                    if (i >= actors.Count)
                    {
                        break;
                    }
                    for (int j = i + 1; j < actors.Count;j++)
                    {

                        
                        if (actors[i].GetAABB().Intersects(actors[j].GetAABB()))
                        {
                           
                            actors[i].isColliding(actors[j]);
                            actors[j].isColliding(actors[i]);

                        }
                         
                    }
            

            }
        //    _inputManager.update(dTime);
            base.update(dTime);

            
        }
        
        public override void draw(SpriteBatch sb)
        {
            //sb.Draw(tipito.tex, tipito.position, Color.White);
            sb.DrawString(_debugFont, _inputManager.getBuffer(), Vector2.Zero, Color.White);
            sb.DrawString(_debugFont, "Last Combo:", new Vector2(0, 15), Color.White);
            sb.DrawString(_debugFont, _inputManager.lastPressedCombo, new Vector2(120, 15), Color.White);
            for (int i = 0; i < actors.Count; i++)
            {
                if (actors[i].isEnable == true)
                {
                    Vector2[] bb= actors[i].GetBB();
                    
                    
                    //Tools.DrawRect(sb, new Vector2(actors[i].AABB.Top, actors[i].AABB.Left), new Vector2(actors[i].AABB.Bottom, actors[i].AABB.Right));
                    for (int j = 0; j < bb.Length - 1; j++)
                    {
                        Tools.DrawLine(bb[j], bb[j + 1], Color.Black, 0, sb);

                    }
                    Tools.DrawLine(bb[3], bb[0], Color.Black, 0, sb);
                    Tools.DrawRect(sb, actors[i].GetAABB());

                    sb.Draw(actors[i].anim.text, actors[i].position, actors[i].anim.getRect(), Color.White, 0, new Vector2(actors[i].anim.w/2,actors[i].anim.h/2), 1, SpriteEffects.None, 0);
                }
            }
                base.draw(sb);
        }

        //se llama desde el sceneManager
        public override void handleInput(InputManager input)
        {
           
           /* if (_inputManager.isNewMouseClick())
            {
              
            }
            if (_inputManager.isNewMouseRelease())
            {
             
            }*/
            base.handleInput();
        }
        //temporal-
        public void CreateActor()
        {
            //actors.Add(new Actor());
        }
        
    }
}
